//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
/////////////////////////////////////////////////////////////////////////////
// Autor Vyacheslav Yermolayev
// http:\\bcdev.narod.ru
// e-mail: yerm@mail.ru
/////////////////////////////////////////////////////////////////////////////

#include<memory>
#include <jpeg.hpp>
#include "UPreviewForm.h"


//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TPreviewForm *PreviewForm;
const int cMaxScale = 800;
const int cMinScale = 25;
//---------------------------------------------------------------------------
__fastcall TPreviewForm::TPreviewForm(TComponent* Owner)
    : TForm(Owner),m_Scale(100), m_EzTwDll(new TDll("EZTW32.dll"))
{
    SaveDialog->DefaultExt = GraphicExtension(__classid(TJPEGImage));
    SaveDialog->Filter = GraphicFilter(__classid(TJPEGImage));
}
//---------------------------------------------------------------------------
void __fastcall TPreviewForm::ZoomInActionExecute(TObject *Sender)
{
        m_Scale *= 2;
        Image->Height *= 2;
        Image->Width  *= 2;
}
//---------------------------------------------------------------------------


void __fastcall TPreviewForm::ZoomOutActionExecute(TObject *Sender)
{
    m_Scale /= 2;
    Image->Height /= 2;
    Image->Width  /= 2;
}
//---------------------------------------------------------------------------

void __fastcall TPreviewForm::ZoomInActionUpdate(TObject *Sender)
{
    ZoomInAction->Enabled = !Image->Picture->Bitmap->Empty && m_Scale < cMaxScale;
}
//---------------------------------------------------------------------------

void __fastcall TPreviewForm::ZoomOutActionUpdate(TObject *Sender)
{
    ZoomOutAction->Enabled = !Image->Picture->Bitmap->Empty &&  m_Scale > cMinScale;
}
//---------------------------------------------------------------------------
void __fastcall TPreviewForm::ScanActionExecute(TObject *Sender)
{
    static TDllStdProc2<HANDLE, HWND, int> AcquireNative(*m_EzTwDll,"TWAIN_AcquireNative");
    static TDllStdProc1<HPALETTE,HANDLE> CreateDibPalette(*m_EzTwDll,"TWAIN_CreateDibPalette");
    static TDllStdProcV8<HDC, int, int, int, int,HANDLE, int, int> DrawDibToDC(*m_EzTwDll,"TWAIN_DrawDibToDC");
    static TDllStdProcV1<HANDLE> FreeNative(*m_EzTwDll,"TWAIN_FreeNative");
    if (HANDLE BMHandle = AcquireNative(Handle,0))
    {
        try
        {
            Graphics::TBitmap* Bitmap =  Image->Picture->Bitmap;
            PBITMAPINFOHEADER Info = (PBITMAPINFOHEADER)GlobalLock(BMHandle);
            m_Width  = 1000 * Info->biWidth/Info->biXPelsPerMeter;
            m_Height = 1000 * Info->biHeight/Info->biYPelsPerMeter; ;
            Bitmap->Palette =  CreateDibPalette(BMHandle);
            Bitmap->Width  = Info->biWidth;
            Bitmap->Height = Info->biHeight;
            DrawDibToDC(Bitmap->Canvas->Handle,0,0,Bitmap->Width,Bitmap->Height,BMHandle,0,0);
        }
        __finally
        {
            FreeNative(BMHandle);
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TPreviewForm::ScanActionUpdate(TObject *Sender)
{
    static TDllStdProc0<int> IsAvailable(*m_EzTwDll,"TWAIN_IsAvailable");
    ScanAction->Enabled =  IsAvailable();
}
//---------------------------------------------------------------------------

void __fastcall TPreviewForm::ActionUpdate(TObject *Sender)
{
    ((TAction*)Sender)->Enabled = !Image->Picture->Bitmap->Empty;
}
//---------------------------------------------------------------------------

void __fastcall TPreviewForm::PrintActionExecute(TObject *Sender)
{
   TPrinter* Printer  =  Printers::Printer();
   int PageWidth   = GetDeviceCaps(Printer->Handle,HORZSIZE);
   int PageHeight  = GetDeviceCaps(Printer->Handle,VERTSIZE);
   TRect Rect(0,0, m_Width  * Printer->PageWidth / PageWidth,
                   m_Height * Printer->PageHeight / PageHeight);
   Printer->BeginDoc();
   Printer->Canvas->StretchDraw(Rect, Image->Picture->Bitmap);
   Printer->EndDoc();
}
//---------------------------------------------------------------------------
void __fastcall TPreviewForm::SaveImageActionExecute(TObject *Sender)
{
  if(SaveDialog->Execute())
  {
   std::auto_ptr<TJPEGImage> Jpeg(new TJPEGImage());
   Jpeg->Assign(Image->Picture->Bitmap);
   Jpeg->SaveToFile(SaveDialog->FileName);
  }
}
//---------------------------------------------------------------------------

